<?
function dbConnect() {
	global $dbHost, $dbUsername, $dbPassword, $dbDatabase;
	
	$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die(mysql_error());
	mysql_select_db($dbDatabase, $db) or die(mysql_error());
	
	$sSql = "SET NAMES utf8";
	mysql_query($sSql) or die(mysql_error());
	
	$sSql = "SET CHARACTER SET utf8";
	mysql_query($sSql) or die(mysql_error());
}


function sToUrl($sText) {
	$sText = strtolower($sText); //	Bort med stora bokstäver
	
	$arrReplace = array(" ", "Å", "Ä", "Ö", "å", "ä", "ö", "/", "'", "*", "(", ")", "[", "]", ".", ",", "+", "\\", "\"", "&");
	$arrReplaceWith = array("_", "a", "a", "o", "a", "a", "o", "_", "_", "x", "_", "-", "_", "_", "_", "_", "_", "_", "_", "_");
	
	$sText = str_replace($arrReplace, $arrReplaceWith, $sText); // Byt bort en massa skumma tecken
	
	$sReturn = "";
	
	// Ta bort alla tecken som inte är a-z_-
	for($i = 0; $i < strlen($sText); $i++) {
		if(preg_match("/[a-z_-]/", $sText{$i})) {
			$sReturn .= $sText{$i};
		}
	}
	
	$sReturn = ereg_replace("__+", "_", $sReturn); // Sök bort alla dubbla eller mer underlines mot en enkel
	
	$sReturn = str_replace("_-_", "-", $sReturn); // Bara en ful kombo som blir ibland, ett bindestreck är snyggare
	
	$sReturn = substr($sReturn, 0, 30); // Max 30 tecken långa urlar får räcka
	
	if($sReturn{0} == "_") { // Börjar urlen med underscore så ska det bort, fult
		$sReturn = substr($sReturn, 1);
	}
	
	if(substr($sReturn, -1) == "_") { // Slutar urlen med underscore så ska den bort, fult
		$sReturn = substr($sReturn, 0, -1);
	}
	
	return $sReturn;
}





/*
function fReturn() {
	return $_SERVER['SCRIPT_NAME'] . "?" . str_replace("&", "@", $_SERVER['QUERY_STRING']);
}

function trimText($sText, $maxLength) {
	if(strlen($sText) > $maxLength) {
		$sText = substr($sText, 0, strrpos(substr($sText, 0, $maxLength), " ")) . "...";
	}
	
	return $sText;
}

function shortUrl($url) {
	$url = urldecode($url);
	$url = strtolower($url);
	$url = str_replace("http://", "", $url);
	$part1 = strlen(substr($url, 0, strpos($url, "/") + 1)) > 20 ? substr($url, 0, 20) : substr($url, 0, strpos($url, "/") + 1) ;
	$part2 = strlen(substr($url, strrpos($url, "/"))) > 20 ? substr($url, -20) : substr($url, strrpos($url, "/"));
	
	$url = $part1 . "..." . $part2;
	
	return($url);
}

function fix_text($sText) {
	$sText = preg_replace("/\[left\](.*?)\[\/left\]/s", "<div align='left'>$1</div>", $sText);
	$sText = preg_replace("/\[center\](.*?)\[\/center\]/s", "<div align='center'>$1</div>", $sText);
	$sText = preg_replace("/\[justified\](.*?)\[\/justified\]/s", "<div style='text-align: justify'>$1</div>", $sText);
	$sText = preg_replace("/\[right\](.*?)\[\/right\]/s", "<div align='right'>$1</div>", $sText);
	
	$sText = preg_replace("/\[size=1\](.*?)\[\/size\]/s", "<span style='FONT-SIZE: 9px; LINE-HEIGHT: 12px'>$1</span>", $sText);
	$sText = preg_replace("/\[size=2\](.*?)\[\/size\]/s", "<span style='FONT-SIZE: 12px; LINE-HEIGHT: 15px'>$1</span>", $sText);
	$sText = preg_replace("/\[size=3\](.*?)\[\/size\]/s", "<span style='FONT-SIZE: 15px; LINE-HEIGHT: 19px'>$1</span>", $sText);
	$sText = preg_replace("/\[size=4\](.*?)\[\/size\]/s", "<span style='FONT-SIZE: 19px; LINE-HEIGHT: 24px'>$1</span>", $sText);
	$sText = preg_replace("/\[size=5\](.*?)\[\/size\]/s", "<span style='FONT-SIZE: 24px; LINE-HEIGHT: 30px'>$1</span>", $sText);
	$sText = preg_replace("/\[size=6\](.*?)\[\/size\]/s", "<span style='FONT-SIZE: 36px; LINE-HEIGHT: 42px'>$1</span>", $sText);

	$sText = str_replace("[line]", "<hr style='border: solid 1px #999999'>", $sText);
	$sText = preg_replace("/\[link=(.+?)\](.+?)\[\/link\]/s", "<a href='$1' target='_blank'>$2</a>", $sText);
	$sText = preg_replace("/\[mail=(.+?)\](.+?)\[\/mail\]/s", "<a href='mailto:$1'>$2</a>", $sText);

	$sText = preg_replace("/\[b\](.*?)\[\/b\]/s", "<b>$1</b>", $sText);
	$sText = preg_replace("/\[i\](.*?)\[\/i\]/s", "<i>$1</i>", $sText);
	$sText = preg_replace("/\[u\](.*?)\[\/u\]/s", "<u>$1</u>", $sText);
	$sText = preg_replace("/\[strike\](.*?)\[\/strike\]/s", "<s>$1</s>", $sText);

	$sText = preg_replace("/(?<=^|\s)((https?|ftps?):\/\/)?(([-_a-zA-Z]+\.)+)([a-zA-Z]{2,8})(\/[^\]\[\s]*)?(?=\s|$)/e","'$1'!=''? '<a href=\"$1$3$5$6\" target=\"_blank\">$1$3$5$6</a>':'<a href=\"http://$3$5$6\" target=\"_blank\">$3$5$6</a>'", $sText);
	
	$sText = preg_replace("/( |\n|\r)([a-zA-Z0-9-_.]+[@][A-Za-z0-9_-]+[.][a-zA-Z]{2,6})/", "$1<a href=\"mailto:$2\">$2</a>", $sText);
	
	$sText = str_replace(":%", " <img src='../bilder/smileys/ikon1.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace(">:D", " <img src='../bilder/smileys/ikon10.gif' height=23 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace("(:D", " <img src='../bilder/smileys/ikon7.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace(":D", " <img src='../bilder/smileys/ikon2.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace("(8)", " <img src='../bilder/smileys/ikon11.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace("8)", " <img src='../bilder/smileys/ikon3.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace(">:(", " <img src='../bilder/smileys/ikon4.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace("|)", " <img src='../bilder/smileys/ikon5.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace(":S", " <img src='../bilder/smileys/ikon6.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace("8o", " <img src='../bilder/smileys/ikon9.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace(":´", " <img src='../bilder/smileys/ikon8.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace(":.", " <img src='../bilder/smileys/ikon12.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	$sText = str_replace(":P", " <img src='../bilder/smileys/ikon13.gif' height=20 width=20 border=0 align='absmiddle'> ", $sText);
	
	$sText = preg_replace("/\[fil=(.*?)\](.*?)\[\/fil\]/s", "<a href=\"../filer/$1\" target='_blank'>$2</a>", $sText);
	$sText = preg_replace("/\[bild=(.*?)\]/s", "<img src=\"../filer/$1\" border=0>", $sText);
	
	$sText = str_replace("\r\n", "<br>\r\n", $sText);	
	
	return $sText;
}

function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
  if (!$using_timestamps) {
    $datefrom = strtotime($datefrom, 0);
    $dateto = strtotime($dateto, 0);
  }
  $difference = $dateto - $datefrom; // Difference in seconds
   
  switch($interval) {
   
    case 'yyyy': // Number of full years

      $years_difference = floor($difference / 31536000);
      if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
        $years_difference--;
      }
      if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
        $years_difference++;
      }
      $datediff = $years_difference;
      break;

    case "q": // Number of full quarters

      $quarters_difference = floor($difference / 8035200);
      while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
        $months_difference++;
      }
      $quarters_difference--;
      $datediff = $quarters_difference;
      break;

    case "m": // Number of full months

      $months_difference = floor($difference / 2678400);
      while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
        $months_difference++;
      }
      $months_difference--;
      $datediff = $months_difference;
      break;

    case 'y': // Difference between day numbers

      $datediff = date("z", $dateto) - date("z", $datefrom);
      break;

    case "d": // Number of full days

      $datediff = floor($difference / 86400);
      break;

    case "w": // Number of full weekdays

      $days_difference = floor($difference / 86400);
      $weeks_difference = floor($days_difference / 7); // Complete weeks
      $first_day = date("w", $datefrom);
      $days_remainder = floor($days_difference % 7);
      $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
      if ($odd_days > 7) { // Sunday
        $days_remainder--;
      }
      if ($odd_days > 6) { // Saturday
        $days_remainder--;
      }
      $datediff = ($weeks_difference * 5) + $days_remainder;
      break;

    case "ww": // Number of full weeks

      $datediff = floor($difference / 604800);
      break;

    case "h": // Number of full hours

      $datediff = floor($difference / 3600);
      break;

    case "n": // Number of full minutes

      $datediff = floor($difference / 60);
      break;

    default: // Number of full seconds (default)

      $datediff = $difference;
      break;
  }    

  return round($datediff);

}

function placeImg($edit, $imgNr, $imgName, $align, $maxW, $maxH, $grayW, $grayH, $bildtext) {
	// Redigeringsläge
	if($edit == true) {
		
		if(is_file("bilder/uploaded/scaled/" . $imgName)) {
			$size = getimagesize("bilder/uploaded/scaled/" . $imgName);
			$sAlign = strlen($align) > 0 ? " align='" . $align . "'" : "";
			$padding = ($align == "right") ? " style='padding-left: 10px; padding-down: 10px'" : "";
			
			$sImg = "<img src='bilder/uploaded/scaled/" . $imgName . "' " . $size[3] . $sAlign . $padding . " border=0>";
		} else {
			$sImg = "<img src='bilder/stomme/gray.gif' width='" . $grayW . "' height='" . $grayH . "' border=0>";
		}
		
		$popupUrl = "editimage.php?imgNr=" . $imgNr . "&imgName=" . $imgName . "&maxW=" . $maxW . "&maxH=" . $maxH . "&grayW=" . $grayW . "&grayH=" . $grayH;
	
		$sHtml = "<div id='bildDiv" . $imgNr . "'>" . 
				 "<a href='#' onClick=\"window.open('" . $popupUrl . "','','width=425,height=550,resizable=yes'); return false;\">" . $sImg . "</a></div>" .
				 "<div id='formBildtext" . $imgNr . "'><textarea rows=3 name='bildtext" . $imgNr . "' style='width: " . $maxW . "px' " .
				 "class='bildtext' onFocus=\"selectedFieldId = " . ($imgNr + 4) . "\">" . $bildtext . "</textarea></div>" .
				 "<div id='preBildtext" . $imgNr . "' align='left' class='bildtext'></div>\r\n";

	// Publikt läge
	} else {		
		
		// Finns inte bilderna får man bara ett break
		if(is_file("bilder/uploaded/scaled/" . $imgName) && is_file("bilder/uploaded/original/" . $imgName)) {
			$size = getimagesize("bilder/uploaded/scaled/" . $imgName);
			$sizeBig = getimagesize("bilder/uploaded/original/" . $imgName);
			
			$sAlign = strlen($align) > 0 ? " align='" . $align . "'" : "";
			$padding = ($align == "right") ? " style='padding-left: 10px; padding-down: 10px'" : "";
			
			$sHtml = "<a href='#' onClick=\"window.open('popup.php?imgName=" . $imgName . "','','width=" . ($sizeBig[0]) . ",height=" . ($sizeBig[1] + 40) . "'); return false;\">" .
					 "<img src='bilder/uploaded/scaled/" . $imgName . "' " . $size[3] . $sAlign . $padding . " border=0></a>";
			
			if(strlen($bildtext) > 0) {
				$sHtml .= "<br><div class='bildtext' align='left'>" . fix_text($bildtext) . "</div>";
			}
		} else {
			$sHtml = "<br>";
		}
		
	}
	
	return $sHtml;
}

function createTitle($edit, $title, $width, $id) {
	if($edit == true) {
		$sHtml = "<div id='formTitleDiv'>
				 <input type='text' name='title' value='" . $title . "' onFocus=\"selectedFieldId = 4\"
				 	style='width: " . $width . "px; border: 1px solid #CCCCCC;' class='blogTitle'></div>
				 <div id='preTitleDiv' style='display: none; width: 425px;'></div>";
	} else {
		$sHtml = "<div id='formTitleDiv'>" . $title . "</div>";
	}
			 
	return $sHtml;
}

function createBody($bodyId, $edit, $body, $width, $height) {
	if($edit) {
		$sHtml = "<div id='formBodyDiv" . $bodyId . "'><textarea name='theBody" . $bodyId . "' rows='" . $height . "' onFocus=\"selectedFieldId = " . $bodyId . "\" style='width: " . $width . "px'>" . $body . "</textarea></div>
				 <div id='preBodyDiv" . $bodyId . "' style='display: none;'></div>";
	} else {
		$sHtml = "<div id='formBodyDiv" . $bodyId . "'>" . fix_text($body) . "</div>";
	}
			
	return $sHtml;
}

function placeVideo($edit, $videoSrc) {
	if($edit) {
		$sHtml = "<div id='videoDiv'>" . $videoSrc . "</div>" .
				 "<div id='formVideoDiv'><br><input type='text' name='videoSrc' value='" . $videoSrc . "' style='width: 425px; border: 1px solid #CCCCCC' " .
				 "onChange=\"document.getElementById('videoDiv').innerHTML = this.value\" " .
				 "onClick=\"this.select();\"></div>";
	} else {
		$sHtml = $videoSrc;
	}
			
	return $sHtml;
}

function createInfo($id, $publicerad, $dateTime, $kommentera, $namn, $antalKommentarer, $edit, $showInfo) {
	$sTime = strftime("%Y-%m-%d %H:%M", strtotime($dateTime));
	$sPublishedBy = "<i>Publicerad " . $sTime . " av " . $namn . "</i>";
	$sComment = "<a href='./?id=" . $id . "'>" . $antalKommentarer . " kommentar(er)</a>";
	
	switch($publicerad) {
	case "0":		
		$sInfo = $kommentera == 1 ? $sComment : "";		
		break;
	case "1":
		$sInfo = $kommentera == 1 ? $sPublishedBy . " - " . $sComment : $sPublishedBy;
		break;
	case "2":
		$sInfo = $kommentera == 1 ? $sTime . ", " . $namn . " - " . $sComment : $sTime . ", " . $namn;
		break;
	case "3":
		$sInfo = $kommentera == 1 ? $sTime . " - " . $sComment : $sTime;
		break;
	}
	
	if(strlen($sInfo) > 0) {
		$sInfo .= "<br><br>";
	}	
	
	// Checkboxen som fixar så man kan välja att inte ha med inforaden
	if(strlen($sInfo) > 0 && isset($_SESSION['komponentAdminId']) && (isset($_GET['id']) || isset($_GET['mId'])) && $edit == true) {
		
		if($showInfo == 1) {
			$sInfo = "<input type='checkbox' name='showInfo' value='1' checked onChange='showHideInfo();'> " .
					 "<span id='formInforad' class='inforad'> " . $sInfo . "</span>" .
					 "<span id='preInforad' style='display: none;' class='infodisabled'>" . $sInfo . "</span>";
		} else {
			$sInfo = "<input type='checkbox' name='showInfo' value='1' onChange='showHideInfo();'> " .
					 "<span id='formInforad' style='display: none;' class='inforad'> " . $sInfo . "</span>" .
					 "<span id='preInforad' class='infodisabled'>" . $sInfo . "</span>";		
		}
	// Om checkboxen inte är iklickad så raderar jag sInfo
	} elseif($showInfo == 0) {
		$sInfo = "";
	}
	
	return $sInfo;
}


*/
?>